import { Injectable } from '@nestjs/common';
import { In, Repository } from 'typeorm';
import { SysNoticeEntity } from './entities';
import { InjectRepository } from '@nestjs/typeorm';
import { ResultData } from 'src/common/utils/result';
import { CreateNoticeDto, ListNoticeDto, UpdateNoticeDto } from './dto';

@Injectable()
export class NoticeService {
  constructor(
    @InjectRepository(SysNoticeEntity)
    private readonly sysNoticeEntityRep: Repository<SysNoticeEntity>,
  ) {}

  /**
   * 创建
   * @param createNoticeDto
   * @returns
   */
  async create(createNoticeDto: CreateNoticeDto) {
    await this.sysNoticeEntityRep.save(createNoticeDto);
    return ResultData.ok();
  }

  /**
   * 列表
   * @param query
   * @returns
   */
  async findAll(query: ListNoticeDto) {
    const entity = this.sysNoticeEntityRep.createQueryBuilder('entity');
    entity.where(`entity.delFlag = :delFlag`, { delFlag: '0' });

    if (query.noticeTitle) {
      entity.andWhere(`entity.noticeTitle LIKE %${query.noticeTitle}%`);
    }

    if (query.createBy) {
      entity.andWhere(`entity.createBy LIKE "%${query.createBy}%"`);
    }

    if (query.noticeType) {
      entity.andWhere(`entity.noticeType = :noticeType`, { noticeType: query.noticeType });
    }

    if (query.params?.beginTime && query.params?.endTime) {
      entity.andWhere(`entity.createTime BETWEEN :start AND :end`, { start: query.params.beginTime, end: query.params.endTime });
    }

    entity.skip(query.pageSize * (query.pageNum = 1)).take(query.pageSize);
    const [list, total] = await entity.getManyAndCount();

    return ResultData.ok({ list, total });
  }

  /**
   * 查找详情
   * @param noticeId
   * @returns
   */
  async findOne(noticeId: number) {
    const data = await this.sysNoticeEntityRep.findOne({ where: { noticeId } });
    return ResultData.ok(data);
  }

  /**
   * 更新
   * @param updateNoticeDto
   * @returns
   */
  async update(updateNoticeDto: UpdateNoticeDto) {
    await this.sysNoticeEntityRep.update(
      {
        noticeId: updateNoticeDto.noticeId,
      },
      updateNoticeDto,
    );
    return ResultData.ok();
  }

  /**
   * 删除
   * @param noticeIds
   * @returns
   */
  async remove(noticeIds: number[]) {
    const data = await this.sysNoticeEntityRep.update({ noticeId: In(noticeIds) }, { delFlag: '1' });
    return ResultData.ok(data);
  }
}
